﻿@using ALinq.BBS.DataAccess;
@{
    //var root = Board.CreateRootInstance();
    var boards = new Boards();
}
@helper  SubBoardsHtml(IEnumerable<Forum> nodes, int padding = 8 - 20)
    {
        const int padding_increase = 20;
        foreach (var child in nodes)
        {
    <div class="boardPanel_item">
        <div class="subCatalog" style="padding-left:@(padding + padding_increase)px;">
            @if (child.Children.Count() > 0)
            {
                <img src="@Url.Content("~/Content/Images/Button/Open.gif")" style="cursor: hand;" alt="-" onclick="CatalogPanel_Expand(this)" /> 
            }
            @Html.RouteLink(child.Name, RouteNames.PostList, new { boardID = child.Board.ID, forumID = child.ID })
        </div>
        @if (child.Children.Count() != 0)
        {
            <div class="boardPanel_item_body">
                @SubBoardsHtml(child.Children, padding + padding_increase)
            </div>
        }
    </div>
        }
}
@foreach (var item in boards)
{
    <div class="catalogPanel">
        <div class="catalogPanelItem">
            @item.Name
        </div>
        @SubBoardsHtml(item.Forums.Where(o => o.Parent == null).ToArray())
    </div>
}
<script type="text/javascript">
    function CatalogPanel_Expand(node) {
        var items = $(node.parentNode).next();
        if (items.length > 0) {
            if ($(items[0]).is(':visible')) {
                $(items[0]).hide();
                node.src = '@Url.Content("~/Content/Images/Button/Close.gif")'
            }
            else {
                $(items[0]).show();
                node.src = '@Url.Content("~/Content/Images/Button/Open.gif")'
            }
        }
    }

    var items = $('div[class="subCatalog"] > img');
    for (var i = items.length - 1; i >= 0; i--) {
        var item = items[i];
        CatalogPanel_Expand(item);
    }
</script>
